package com.poi.tl.sample.quickstart.utils;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class PdfUtils {

  public static void toPdf(String inPath, String outPath){
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream=null;
    try {
      // 读取docx文件
      fileInputStream = new FileInputStream(inPath);
      XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
      PdfOptions pdfOptions = PdfOptions.create();
      // 输出路径
      fileOutputStream = new FileOutputStream(outPath);
      // 调用转换
      PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      IOUtils.closeQuietly(fileInputStream);
      IOUtils.closeQuietly(fileOutputStream);
    }
  }

  public static void main(String[] args) {
    String fullDocxPath = "E:\\export/_7_1main_template_output.docx";
    String fullPdfPath = "E:\\export/_7_1main_template_output.pdf";
    toPdf(fullDocxPath, fullPdfPath);
  }

}
